package com.jianshuge.app.adapter;

import java.util.List;

import com.jianshuge.app.R;
import com.jianshuge.app.bean.Booklist;
import com.jianshuge.app.bean.BooklistItem;
import com.jianshuge.app.common.BitmapManager;
import com.jianshuge.app.common.StringUtils;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewBooklistAdapter extends MyBaseAdapter {

	private Context 					context;//运行上下文
	private List<Booklist> 				listItems;//数据集合
	private LayoutInflater 				listContainer;//视图容器
	private int 						itemViewResource;//自定义项视图源 
	private BitmapManager 				bmpManager;
	private int						booklist_type;
	
	static class ListItemView { // 自定义控件集合
		public TextView title;
	//	public TextView desc;
		public ImageView book_image1;
		public ImageView book_image2;
		public ImageView book_image3;
	}

	/**
	 * 实例化Adapter
	 * 
	 * @param context
	 * @param data
	 * @param resource
	 * @param isFaceClickEnable
	 */
	public ListViewBooklistAdapter(Context context, int booklist_type, List<Booklist> data,int resource) {
		this.context = context;			
		this.listContainer = LayoutInflater.from(context);	//创建视图容器并设置上下文
		this.itemViewResource = resource;
		this.listItems = data;
		this.bmpManager = new BitmapManager(BitmapFactory.decodeResource(context.getResources(), R.drawable.default_book_pic));
		this.booklist_type = booklist_type;
	}

	public int getCount() {
		return listItems.size();
	}

	public Object getItem(int arg0) {
		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}
	
	/**
	 * ListView Item设置
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		//Log.d("method", "getView");
		
		//自定义视图
		ListItemView  listItemView = null;
		
		if (convertView == null) {
			//获取list_item布局文件的视图
			convertView = listContainer.inflate(this.itemViewResource, null);
			
			listItemView = new ListItemView();
			//获取控件对象
			listItemView.title = (TextView)convertView.findViewById(R.id.booklist_listitem_title);
			//listItemView.desc = (TextView)convertView.findViewById(R.id.booklist_listitem_desc);
			listItemView.book_image1 = (ImageView)convertView.findViewById(R.id.booklist_listitem_img1);
			listItemView.book_image2 = (ImageView)convertView.findViewById(R.id.booklist_listitem_img2);
			listItemView.book_image3 = (ImageView)convertView.findViewById(R.id.booklist_listitem_img3);
			
			//设置控件集到convertView
			convertView.setTag(listItemView);
		}else {
			listItemView = (ListItemView)convertView.getTag();
		}	
		
		//设置文字和图片
		Booklist booklist = listItems.get(position);
		
		listItemView.title.setText( booklist.getTitle() );
		listItemView.title.setTag( booklist );//设置隐藏参数(实体类)
		//listItemView.desc.setText( booklist.getDescs() );
		
		List<BooklistItem> booklist_items = booklist.getBooklistItems();

		if( !booklist_items.isEmpty() ) {
			
			int len = booklist_items.size();
			
			if( len >= 3 ) {
			
				BooklistItem booklist_item = booklist_items.get(0);
				String book_image_url = booklist_item.getBookImageUrl();
				
				if(book_image_url.endsWith("portrait.gif") || StringUtils.isEmpty(book_image_url)){
					listItemView.book_image1.setImageResource(R.drawable.default_book_pic);
				}else{
					bmpManager.loadBitmap(book_image_url, listItemView.book_image1 );
				}
			
			
				booklist_item = booklist_items.get(1);
				book_image_url = booklist_item.getBookImageUrl();
				
				if(book_image_url.endsWith("portrait.gif") || StringUtils.isEmpty(book_image_url)){
					listItemView.book_image2.setImageResource(R.drawable.default_book_pic);
				}else{
					bmpManager.loadBitmap(book_image_url, listItemView.book_image2 );
				}
			
				booklist_item = booklist_items.get(2);
				book_image_url = booklist_item.getBookImageUrl();
				
				if(book_image_url.endsWith("portrait.gif") || StringUtils.isEmpty(book_image_url)){
					listItemView.book_image3.setImageResource(R.drawable.default_book_pic);
				}else{
					bmpManager.loadBitmap(book_image_url, listItemView.book_image3 );
				}
			}
			else
			{
				if( len == 2 )
				{
					BooklistItem booklist_item = booklist_items.get(0);
					String book_image_url = booklist_item.getBookImageUrl();
					
					if(book_image_url.endsWith("portrait.gif") || StringUtils.isEmpty(book_image_url)){
						listItemView.book_image1.setImageResource(R.drawable.default_book_pic);
					}else{
						bmpManager.loadBitmap(book_image_url, listItemView.book_image1 );
					}
				
					booklist_item = booklist_items.get(1);
					book_image_url = booklist_item.getBookImageUrl();
					
					if(book_image_url.endsWith("portrait.gif") || StringUtils.isEmpty(book_image_url)){
						listItemView.book_image2.setImageResource(R.drawable.default_book_pic);
					}else{
						bmpManager.loadBitmap(book_image_url, listItemView.book_image2 );
					}
					
					listItemView.book_image3.setImageResource(R.drawable.default_book_pic);
			
				}
				
				if( len == 1)
				{
					BooklistItem booklist_item = booklist_items.get(0);
					String book_image_url = booklist_item.getBookImageUrl();
					
					if(book_image_url.endsWith("portrait.gif") || StringUtils.isEmpty(book_image_url)){
						listItemView.book_image1.setImageResource(R.drawable.default_book_pic);
					}else{
						bmpManager.loadBitmap(book_image_url, listItemView.book_image1 );
					}
					
					listItemView.book_image2.setImageResource(R.drawable.default_book_pic);
					listItemView.book_image3.setImageResource(R.drawable.default_book_pic);
				}
			}
		} else {
			
			listItemView.book_image1.setImageResource(R.drawable.default_book_pic);
			listItemView.book_image2.setImageResource(R.drawable.default_book_pic);
			listItemView.book_image3.setImageResource(R.drawable.default_book_pic);	
		
		}
		
		return convertView;
	}
}